﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Olemas.DataModels;
using Olemas.Models;
using Olemas.ViewModels;

namespace Olemas.Controllers
{
    public class ConspectsController : Controller
    {
        private const int PageSize = 8;
        private ConspectsViewModel _conspectsViewModel;

        //
        // GET: /Conspects/Ehitusteaduskond || GET: /Conspects/
        /// <summary>
        /// Returns the main view for the conspects.
        /// </summary>
        /// <param name="fac">The name of the faculty to display conspects for.</param>
        [OutputCache(Duration = 60)]
        public ActionResult Index(string fac)
        {
            var currentFacultyId = FacultyModel.GetFacultyId(fac);
            if (fac != null && currentFacultyId == 0)
            {
                return new HttpNotFoundResult();
            }
            var totalConspects = ConspectModel.GetNumberOfConspects(currentFacultyId);

            var totalPages = GlobalViewModel.GetTotalPages(totalConspects, PageSize);

            var pagesRemaining = GlobalViewModel.RemainingPages(totalPages, 1);

            _conspectsViewModel = new ConspectsViewModel(null, currentFacultyId, 1, totalPages, pagesRemaining);
            return View("Index", _conspectsViewModel);

        }

        /// <summary>
        /// Returns partial view of recentConspects to be updated with ajax.
        /// </summary>
        /// <param name="page">The page number to load conspects for.</param>
        /// <param name="facId">The Id of the faculty to load the conspects for.</param>
        /// <param name="ordering">What to order the books by.</param>
        public ActionResult RecentConspects(int page, int facId, string ordering)
        {
            var conspects = ConspectModel.GetConspects(facId, page, PageSize, ordering) as IEnumerable<Conspect>;

            var totalBooks = ConspectModel.GetNumberOfConspects(facId);

            var totalPages = GlobalViewModel.GetTotalPages(totalBooks, PageSize);

            var pagesRemaining = GlobalViewModel.RemainingPages(totalPages, page);

            _conspectsViewModel = new ConspectsViewModel(conspects, facId, page, totalPages, pagesRemaining);
            return PartialView("_RecentConspects", _conspectsViewModel);
        }

    }
}
